package com.example.wechatapi;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @date 2021/5/7 -11:07
 */
@Service
public class WechatServiceImpl implements WechatService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Environment env;

    @Async
    @Override
    public void push() {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(env.getProperty("wechat.appId"));
        wxStorage.setSecret(env.getProperty("wechat.secret"));
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        List<String> openIdList = getOpenIdList();

        for (String openId : openIdList) {

            String templateId = env.getProperty("wechat.templateId");
            String url = env.getProperty("wechat.url");
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId(templateId)
                    .url(url)
                    .build();

            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sf.format(new Date());

            //3,如果是正式版发送模版消息，这里需要配置你的信息
            templateMessage.addData(new WxMpTemplateData("kw1", "4934！", "#173177"));
            templateMessage.addData(new WxMpTemplateData("kw2", format, "#173177"));
            templateMessage.addData(new WxMpTemplateData("kw3", "工资收入", "#173177"));
            templateMessage.addData(new WxMpTemplateData("kw4", "37000000", "#173177"));
            templateMessage.addData(new WxMpTemplateData("kw5", "银行转账", "#173177"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                logger.info("推送完成：{}", openId);
            } catch (Exception e) {
                logger.error("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    private List<String> getOpenIdList() {
        List<String> openIdList = new ArrayList<>();
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        openIdList.add("oaaCK5_aVLrittWH7ECamYI251sE");
        return openIdList;

    }
}
